{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# SETUP\n",
    "#\n",
    "# Install keras R\n",
    "# install.packages('keras', repos = \"https://cloud.r-project.org\")\n",
    "# \n",
    "# Update reticulate from cran (it defaults to mran which has an outdated version)\n",
    "# install.packages(\"reticulate\", repos = \"https://cloud.r-project.org\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Loading required package: rjson\n"
     ]
    }
   ],
   "source": [
    "library(keras)\n",
    "use_python('/anaconda/envs/py35')\n",
    "\n",
    "# Import util functions\n",
    "source(\"./common/utils.R\")\n",
    "\n",
    "# Import hyper-parameters\n",
    "params <- load_params(\"inference\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# reticulate::py_config()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "OS: Linux \n",
      "R version 3.4.1 (2017-06-30) \n",
      "Keras: 2.1.5 \n",
      "Tensorflow: 1.5 \n",
      "Keras using tensorflow \n",
      "Keras channel ordering is channels_last \n",
      "GPU:  Tesla P100-PCIE-16GB \n",
      "CUDA Version 8.0.61 \n",
      "CuDNN Version 6.0.21 \n"
     ]
    }
   ],
   "source": [
    "cat(\"OS:\", Sys.info()[\"sysname\"], \"\\n\")\n",
    "cat(R.version$version.string, \"\\n\")\n",
    "cat(\"Keras:\", paste0(packageVersion(\"keras\")), \"\\n\")\n",
    "cat(\"Tensorflow:\", paste0(packageVersion(\"tensorflow\")), \"\\n\")\n",
    "cat(\"Keras using\", backend()$backend(), \"\\n\")\n",
    "cat(\"Keras channel ordering is\", backend()$image_data_format(), \"\\n\") \n",
    "cat(\"GPU: \", get_gpu_name(), \"\\n\")\n",
    "cat(get_cuda_version(), \"\\n\")\n",
    "cat(get_cudnn_version(), \"\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Generate some fake data for inference\n",
    "fake_data <- give_fake_data(batches = params$BATCH_SIZE * params$BATCHES_GPU, col_major = FALSE)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x_train shape: 1280 224 224 3 \n"
     ]
    }
   ],
   "source": [
    "cat('x_train shape:', dim(fake_data), '\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Instantiate the model\n",
    "model <- application_resnet50(include_top = FALSE, input_shape = c(224L, 224L, 3L))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "________________________________________________________________________________\n",
      "Layer (type)              Output Shape      Param #  Connected to               \n",
      "================================================================================\n",
      "input_1 (InputLayer)      (None, 224, 224,  0                                   \n",
      "________________________________________________________________________________\n",
      "conv1_pad (ZeroPadding2D) (None, 230, 230,  0        input_1[0][0]              \n",
      "________________________________________________________________________________\n",
      "conv1 (Conv2D)            (None, 112, 112,  9472     conv1_pad[0][0]            \n",
      "________________________________________________________________________________\n",
      "bn_conv1 (BatchNormalizat (None, 112, 112,  256      conv1[0][0]                \n",
      "________________________________________________________________________________\n",
      "activation_1 (Activation) (None, 112, 112,  0        bn_conv1[0][0]             \n",
      "________________________________________________________________________________\n",
      "max_pooling2d_1 (MaxPooli (None, 55, 55, 64 0        activation_1[0][0]         \n",
      "________________________________________________________________________________\n",
      "res2a_branch2a (Conv2D)   (None, 55, 55, 64 4160     max_pooling2d_1[0][0]      \n",
      "________________________________________________________________________________\n",
      "bn2a_branch2a (BatchNorma (None, 55, 55, 64 256      res2a_branch2a[0][0]       \n",
      "________________________________________________________________________________\n",
      "activation_2 (Activation) (None, 55, 55, 64 0        bn2a_branch2a[0][0]        \n",
      "________________________________________________________________________________\n",
      "res2a_branch2b (Conv2D)   (None, 55, 55, 64 36928    activation_2[0][0]         \n",
      "________________________________________________________________________________\n",
      "bn2a_branch2b (BatchNorma (None, 55, 55, 64 256      res2a_branch2b[0][0]       \n",
      "________________________________________________________________________________\n",
      "activation_3 (Activation) (None, 55, 55, 64 0        bn2a_branch2b[0][0]        \n",
      "________________________________________________________________________________\n",
      "res2a_branch2c (Conv2D)   (None, 55, 55, 25 16640    activation_3[0][0]         \n",
      "________________________________________________________________________________\n",
      "res2a_branch1 (Conv2D)    (None, 55, 55, 25 16640    max_pooling2d_1[0][0]      \n",
      "________________________________________________________________________________\n",
      "bn2a_branch2c (BatchNorma (None, 55, 55, 25 1024     res2a_branch2c[0][0]       \n",
      "________________________________________________________________________________\n",
      "bn2a_branch1 (BatchNormal (None, 55, 55, 25 1024     res2a_branch1[0][0]        \n",
      "________________________________________________________________________________\n",
      "add_1 (Add)               (None, 55, 55, 25 0        bn2a_branch2c[0][0]        \n",
      "                                                     bn2a_branch1[0][0]         \n",
      "________________________________________________________________________________\n",
      "activation_4 (Activation) (None, 55, 55, 25 0        add_1[0][0]                \n",
      "________________________________________________________________________________\n",
      "res2b_branch2a (Conv2D)   (None, 55, 55, 64 16448    activation_4[0][0]         \n",
      "________________________________________________________________________________\n",
      "bn2b_branch2a (BatchNorma (None, 55, 55, 64 256      res2b_branch2a[0][0]       \n",
      "________________________________________________________________________________\n",
      "activation_5 (Activation) (None, 55, 55, 64 0        bn2b_branch2a[0][0]        \n",
      "________________________________________________________________________________\n",
      "res2b_branch2b (Conv2D)   (None, 55, 55, 64 36928    activation_5[0][0]         \n",
      "________________________________________________________________________________\n",
      "bn2b_branch2b (BatchNorma (None, 55, 55, 64 256      res2b_branch2b[0][0]       \n",
      "________________________________________________________________________________\n",
      "activation_6 (Activation) (None, 55, 55, 64 0        bn2b_branch2b[0][0]        \n",
      "________________________________________________________________________________\n",
      "res2b_branch2c (Conv2D)   (None, 55, 55, 25 16640    activation_6[0][0]         \n",
      "________________________________________________________________________________\n",
      "bn2b_branch2c (BatchNorma (None, 55, 55, 25 1024     res2b_branch2c[0][0]       \n",
      "________________________________________________________________________________\n",
      "add_2 (Add)               (None, 55, 55, 25 0        bn2b_branch2c[0][0]        \n",
      "                                                     activation_4[0][0]         \n",
      "________________________________________________________________________________\n",
      "activation_7 (Activation) (None, 55, 55, 25 0        add_2[0][0]                \n",
      "________________________________________________________________________________\n",
      "res2c_branch2a (Conv2D)   (None, 55, 55, 64 16448    activation_7[0][0]         \n",
      "________________________________________________________________________________\n",
      "bn2c_branch2a (BatchNorma (None, 55, 55, 64 256      res2c_branch2a[0][0]       \n",
      "________________________________________________________________________________\n",
      "activation_8 (Activation) (None, 55, 55, 64 0        bn2c_branch2a[0][0]        \n",
      "________________________________________________________________________________\n",
      "res2c_branch2b (Conv2D)   (None, 55, 55, 64 36928    activation_8[0][0]         \n",
      "________________________________________________________________________________\n",
      "bn2c_branch2b (BatchNorma (None, 55, 55, 64 256      res2c_branch2b[0][0]       \n",
      "________________________________________________________________________________\n",
      "activation_9 (Activation) (None, 55, 55, 64 0        bn2c_branch2b[0][0]        \n",
      "________________________________________________________________________________\n",
      "res2c_branch2c (Conv2D)   (None, 55, 55, 25 16640    activation_9[0][0]         \n",
      "________________________________________________________________________________\n",
      "bn2c_branch2c (BatchNorma (None, 55, 55, 25 1024     res2c_branch2c[0][0]       \n",
      "________________________________________________________________________________\n",
      "add_3 (Add)               (None, 55, 55, 25 0        bn2c_branch2c[0][0]        \n",
      "                                                     activation_7[0][0]         \n",
      "________________________________________________________________________________\n",
      "activation_10 (Activation (None, 55, 55, 25 0        add_3[0][0]                \n",
      "________________________________________________________________________________\n",
      "res3a_branch2a (Conv2D)   (None, 28, 28, 12 32896    activation_10[0][0]        \n",
      "________________________________________________________________________________\n",
      "bn3a_branch2a (BatchNorma (None, 28, 28, 12 512      res3a_branch2a[0][0]       \n",
      "________________________________________________________________________________\n",
      "activation_11 (Activation (None, 28, 28, 12 0        bn3a_branch2a[0][0]        \n",
      "________________________________________________________________________________\n",
      "res3a_branch2b (Conv2D)   (None, 28, 28, 12 147584   activation_11[0][0]        \n",
      "________________________________________________________________________________\n",
      "bn3a_branch2b (BatchNorma (None, 28, 28, 12 512      res3a_branch2b[0][0]       \n",
      "________________________________________________________________________________\n",
      "activation_12 (Activation (None, 28, 28, 12 0        bn3a_branch2b[0][0]        \n",
      "________________________________________________________________________________\n",
      "res3a_branch2c (Conv2D)   (None, 28, 28, 51 66048    activation_12[0][0]        \n",
      "________________________________________________________________________________\n",
      "res3a_branch1 (Conv2D)    (None, 28, 28, 51 131584   activation_10[0][0]        \n",
      "________________________________________________________________________________\n",
      "bn3a_branch2c (BatchNorma (None, 28, 28, 51 2048     res3a_branch2c[0][0]       \n",
      "________________________________________________________________________________\n",
      "bn3a_branch1 (BatchNormal (None, 28, 28, 51 2048     res3a_branch1[0][0]        \n",
      "________________________________________________________________________________\n",
      "add_4 (Add)               (None, 28, 28, 51 0        bn3a_branch2c[0][0]        \n",
      "                                                     bn3a_branch1[0][0]         \n",
      "________________________________________________________________________________\n",
      "activation_13 (Activation (None, 28, 28, 51 0        add_4[0][0]                \n",
      "________________________________________________________________________________\n",
      "res3b_branch2a (Conv2D)   (None, 28, 28, 12 65664    activation_13[0][0]        \n",
      "________________________________________________________________________________\n",
      "bn3b_branch2a (BatchNorma (None, 28, 28, 12 512      res3b_branch2a[0][0]       \n",
      "________________________________________________________________________________\n",
      "activation_14 (Activation (None, 28, 28, 12 0        bn3b_branch2a[0][0]        \n",
      "________________________________________________________________________________\n",
      "res3b_branch2b (Conv2D)   (None, 28, 28, 12 147584   activation_14[0][0]        \n",
      "________________________________________________________________________________\n",
      "bn3b_branch2b (BatchNorma (None, 28, 28, 12 512      res3b_branch2b[0][0]       \n",
      "________________________________________________________________________________\n",
      "activation_15 (Activation (None, 28, 28, 12 0        bn3b_branch2b[0][0]        \n",
      "________________________________________________________________________________\n",
      "res3b_branch2c (Conv2D)   (None, 28, 28, 51 66048    activation_15[0][0]        \n",
      "________________________________________________________________________________\n",
      "bn3b_branch2c (BatchNorma (None, 28, 28, 51 2048     res3b_branch2c[0][0]       \n",
      "________________________________________________________________________________\n",
      "add_5 (Add)               (None, 28, 28, 51 0        bn3b_branch2c[0][0]        \n",
      "                                                     activation_13[0][0]        \n",
      "________________________________________________________________________________\n",
      "activation_16 (Activation (None, 28, 28, 51 0        add_5[0][0]                \n",
      "________________________________________________________________________________\n",
      "res3c_branch2a (Conv2D)   (None, 28, 28, 12 65664    activation_16[0][0]        \n",
      "________________________________________________________________________________\n",
      "bn3c_branch2a (BatchNorma (None, 28, 28, 12 512      res3c_branch2a[0][0]       \n",
      "________________________________________________________________________________\n",
      "activation_17 (Activation (None, 28, 28, 12 0        bn3c_branch2a[0][0]        \n",
      "________________________________________________________________________________\n",
      "res3c_branch2b (Conv2D)   (None, 28, 28, 12 147584   activation_17[0][0]        \n",
      "________________________________________________________________________________\n",
      "bn3c_branch2b (BatchNorma (None, 28, 28, 12 512      res3c_branch2b[0][0]       \n",
      "________________________________________________________________________________\n",
      "activation_18 (Activation (None, 28, 28, 12 0        bn3c_branch2b[0][0]        \n",
      "________________________________________________________________________________\n",
      "res3c_branch2c (Conv2D)   (None, 28, 28, 51 66048    activation_18[0][0]        \n",
      "________________________________________________________________________________\n",
      "bn3c_branch2c (BatchNorma (None, 28, 28, 51 2048     res3c_branch2c[0][0]       \n",
      "________________________________________________________________________________\n",
      "add_6 (Add)               (None, 28, 28, 51 0        bn3c_branch2c[0][0]        \n",
      "                                                     activation_16[0][0]        \n",
      "________________________________________________________________________________\n",
      "activation_19 (Activation (None, 28, 28, 51 0        add_6[0][0]                \n",
      "________________________________________________________________________________\n",
      "res3d_branch2a (Conv2D)   (None, 28, 28, 12 65664    activation_19[0][0]        \n",
      "________________________________________________________________________________\n",
      "bn3d_branch2a (BatchNorma (None, 28, 28, 12 512      res3d_branch2a[0][0]       \n",
      "________________________________________________________________________________\n",
      "activation_20 (Activation (None, 28, 28, 12 0        bn3d_branch2a[0][0]        \n",
      "________________________________________________________________________________\n",
      "res3d_branch2b (Conv2D)   (None, 28, 28, 12 147584   activation_20[0][0]        \n",
      "________________________________________________________________________________\n",
      "bn3d_branch2b (BatchNorma (None, 28, 28, 12 512      res3d_branch2b[0][0]       \n",
      "________________________________________________________________________________\n",
      "activation_21 (Activation (None, 28, 28, 12 0        bn3d_branch2b[0][0]        \n",
      "________________________________________________________________________________\n",
      "res3d_branch2c (Conv2D)   (None, 28, 28, 51 66048    activation_21[0][0]        \n",
      "________________________________________________________________________________\n",
      "bn3d_branch2c (BatchNorma (None, 28, 28, 51 2048     res3d_branch2c[0][0]       \n",
      "________________________________________________________________________________\n",
      "add_7 (Add)               (None, 28, 28, 51 0        bn3d_branch2c[0][0]        \n",
      "                                                     activation_19[0][0]        \n",
      "________________________________________________________________________________\n",
      "activation_22 (Activation (None, 28, 28, 51 0        add_7[0][0]                \n",
      "________________________________________________________________________________\n",
      "res4a_branch2a (Conv2D)   (None, 14, 14, 25 131328   activation_22[0][0]        \n",
      "________________________________________________________________________________\n",
      "bn4a_branch2a (BatchNorma (None, 14, 14, 25 1024     res4a_branch2a[0][0]       \n",
      "________________________________________________________________________________\n",
      "activation_23 (Activation (None, 14, 14, 25 0        bn4a_branch2a[0][0]        \n",
      "________________________________________________________________________________\n",
      "res4a_branch2b (Conv2D)   (None, 14, 14, 25 590080   activation_23[0][0]        \n",
      "________________________________________________________________________________\n",
      "bn4a_branch2b (BatchNorma (None, 14, 14, 25 1024     res4a_branch2b[0][0]       \n",
      "________________________________________________________________________________\n",
      "activation_24 (Activation (None, 14, 14, 25 0        bn4a_branch2b[0][0]        \n",
      "________________________________________________________________________________\n",
      "res4a_branch2c (Conv2D)   (None, 14, 14, 10 263168   activation_24[0][0]        \n",
      "________________________________________________________________________________\n",
      "res4a_branch1 (Conv2D)    (None, 14, 14, 10 525312   activation_22[0][0]        \n",
      "________________________________________________________________________________\n",
      "bn4a_branch2c (BatchNorma (None, 14, 14, 10 4096     res4a_branch2c[0][0]       \n",
      "________________________________________________________________________________\n",
      "bn4a_branch1 (BatchNormal (None, 14, 14, 10 4096     res4a_branch1[0][0]        \n",
      "________________________________________________________________________________\n",
      "add_8 (Add)               (None, 14, 14, 10 0        bn4a_branch2c[0][0]        \n",
      "                                                     bn4a_branch1[0][0]         \n",
      "________________________________________________________________________________\n",
      "activation_25 (Activation (None, 14, 14, 10 0        add_8[0][0]                \n",
      "________________________________________________________________________________\n",
      "res4b_branch2a (Conv2D)   (None, 14, 14, 25 262400   activation_25[0][0]        \n",
      "________________________________________________________________________________\n",
      "bn4b_branch2a (BatchNorma (None, 14, 14, 25 1024     res4b_branch2a[0][0]       \n",
      "________________________________________________________________________________\n",
      "activation_26 (Activation (None, 14, 14, 25 0        bn4b_branch2a[0][0]        \n",
      "________________________________________________________________________________\n",
      "res4b_branch2b (Conv2D)   (None, 14, 14, 25 590080   activation_26[0][0]        \n",
      "________________________________________________________________________________\n",
      "bn4b_branch2b (BatchNorma (None, 14, 14, 25 1024     res4b_branch2b[0][0]       \n",
      "________________________________________________________________________________\n",
      "activation_27 (Activation (None, 14, 14, 25 0        bn4b_branch2b[0][0]        \n",
      "________________________________________________________________________________\n",
      "res4b_branch2c (Conv2D)   (None, 14, 14, 10 263168   activation_27[0][0]        \n",
      "________________________________________________________________________________\n",
      "bn4b_branch2c (BatchNorma (None, 14, 14, 10 4096     res4b_branch2c[0][0]       \n",
      "________________________________________________________________________________\n",
      "add_9 (Add)               (None, 14, 14, 10 0        bn4b_branch2c[0][0]        \n",
      "                                                     activation_25[0][0]        \n",
      "________________________________________________________________________________\n",
      "activation_28 (Activation (None, 14, 14, 10 0        add_9[0][0]                \n",
      "________________________________________________________________________________\n",
      "res4c_branch2a (Conv2D)   (None, 14, 14, 25 262400   activation_28[0][0]        \n",
      "________________________________________________________________________________\n",
      "bn4c_branch2a (BatchNorma (None, 14, 14, 25 1024     res4c_branch2a[0][0]       \n",
      "________________________________________________________________________________\n",
      "activation_29 (Activation (None, 14, 14, 25 0        bn4c_branch2a[0][0]        \n",
      "________________________________________________________________________________\n",
      "res4c_branch2b (Conv2D)   (None, 14, 14, 25 590080   activation_29[0][0]        \n",
      "________________________________________________________________________________\n",
      "bn4c_branch2b (BatchNorma (None, 14, 14, 25 1024     res4c_branch2b[0][0]       \n",
      "________________________________________________________________________________\n",
      "activation_30 (Activation (None, 14, 14, 25 0        bn4c_branch2b[0][0]        \n",
      "________________________________________________________________________________\n",
      "res4c_branch2c (Conv2D)   (None, 14, 14, 10 263168   activation_30[0][0]        \n",
      "________________________________________________________________________________\n",
      "bn4c_branch2c (BatchNorma (None, 14, 14, 10 4096     res4c_branch2c[0][0]       \n",
      "________________________________________________________________________________\n",
      "add_10 (Add)              (None, 14, 14, 10 0        bn4c_branch2c[0][0]        \n",
      "                                                     activation_28[0][0]        \n",
      "________________________________________________________________________________\n",
      "activation_31 (Activation (None, 14, 14, 10 0        add_10[0][0]               \n",
      "________________________________________________________________________________\n",
      "res4d_branch2a (Conv2D)   (None, 14, 14, 25 262400   activation_31[0][0]        \n",
      "________________________________________________________________________________\n",
      "bn4d_branch2a (BatchNorma (None, 14, 14, 25 1024     res4d_branch2a[0][0]       \n",
      "________________________________________________________________________________\n",
      "activation_32 (Activation (None, 14, 14, 25 0        bn4d_branch2a[0][0]        \n",
      "________________________________________________________________________________\n",
      "res4d_branch2b (Conv2D)   (None, 14, 14, 25 590080   activation_32[0][0]        \n",
      "________________________________________________________________________________\n",
      "bn4d_branch2b (BatchNorma (None, 14, 14, 25 1024     res4d_branch2b[0][0]       \n",
      "________________________________________________________________________________\n",
      "activation_33 (Activation (None, 14, 14, 25 0        bn4d_branch2b[0][0]        \n",
      "________________________________________________________________________________\n",
      "res4d_branch2c (Conv2D)   (None, 14, 14, 10 263168   activation_33[0][0]        \n",
      "________________________________________________________________________________\n",
      "bn4d_branch2c (BatchNorma (None, 14, 14, 10 4096     res4d_branch2c[0][0]       \n",
      "________________________________________________________________________________\n",
      "add_11 (Add)              (None, 14, 14, 10 0        bn4d_branch2c[0][0]        \n",
      "                                                     activation_31[0][0]        \n",
      "________________________________________________________________________________\n",
      "activation_34 (Activation (None, 14, 14, 10 0        add_11[0][0]               \n",
      "________________________________________________________________________________\n",
      "res4e_branch2a (Conv2D)   (None, 14, 14, 25 262400   activation_34[0][0]        \n",
      "________________________________________________________________________________\n",
      "bn4e_branch2a (BatchNorma (None, 14, 14, 25 1024     res4e_branch2a[0][0]       \n",
      "________________________________________________________________________________\n",
      "activation_35 (Activation (None, 14, 14, 25 0        bn4e_branch2a[0][0]        \n",
      "________________________________________________________________________________\n",
      "res4e_branch2b (Conv2D)   (None, 14, 14, 25 590080   activation_35[0][0]        \n",
      "________________________________________________________________________________\n",
      "bn4e_branch2b (BatchNorma (None, 14, 14, 25 1024     res4e_branch2b[0][0]       \n",
      "________________________________________________________________________________\n",
      "activation_36 (Activation (None, 14, 14, 25 0        bn4e_branch2b[0][0]        \n",
      "________________________________________________________________________________\n",
      "res4e_branch2c (Conv2D)   (None, 14, 14, 10 263168   activation_36[0][0]        \n",
      "________________________________________________________________________________\n",
      "bn4e_branch2c (BatchNorma (None, 14, 14, 10 4096     res4e_branch2c[0][0]       \n",
      "________________________________________________________________________________\n",
      "add_12 (Add)              (None, 14, 14, 10 0        bn4e_branch2c[0][0]        \n",
      "                                                     activation_34[0][0]        \n",
      "________________________________________________________________________________\n",
      "activation_37 (Activation (None, 14, 14, 10 0        add_12[0][0]               \n",
      "________________________________________________________________________________\n",
      "res4f_branch2a (Conv2D)   (None, 14, 14, 25 262400   activation_37[0][0]        \n",
      "________________________________________________________________________________\n",
      "bn4f_branch2a (BatchNorma (None, 14, 14, 25 1024     res4f_branch2a[0][0]       \n",
      "________________________________________________________________________________\n",
      "activation_38 (Activation (None, 14, 14, 25 0        bn4f_branch2a[0][0]        \n",
      "________________________________________________________________________________\n",
      "res4f_branch2b (Conv2D)   (None, 14, 14, 25 590080   activation_38[0][0]        \n",
      "________________________________________________________________________________\n",
      "bn4f_branch2b (BatchNorma (None, 14, 14, 25 1024     res4f_branch2b[0][0]       \n",
      "________________________________________________________________________________\n",
      "activation_39 (Activation (None, 14, 14, 25 0        bn4f_branch2b[0][0]        \n",
      "________________________________________________________________________________\n",
      "res4f_branch2c (Conv2D)   (None, 14, 14, 10 263168   activation_39[0][0]        \n",
      "________________________________________________________________________________\n",
      "bn4f_branch2c (BatchNorma (None, 14, 14, 10 4096     res4f_branch2c[0][0]       \n",
      "________________________________________________________________________________\n",
      "add_13 (Add)              (None, 14, 14, 10 0        bn4f_branch2c[0][0]        \n",
      "                                                     activation_37[0][0]        \n",
      "________________________________________________________________________________\n",
      "activation_40 (Activation (None, 14, 14, 10 0        add_13[0][0]               \n",
      "________________________________________________________________________________\n",
      "res5a_branch2a (Conv2D)   (None, 7, 7, 512) 524800   activation_40[0][0]        \n",
      "________________________________________________________________________________\n",
      "bn5a_branch2a (BatchNorma (None, 7, 7, 512) 2048     res5a_branch2a[0][0]       \n",
      "________________________________________________________________________________\n",
      "activation_41 (Activation (None, 7, 7, 512) 0        bn5a_branch2a[0][0]        \n",
      "________________________________________________________________________________\n",
      "res5a_branch2b (Conv2D)   (None, 7, 7, 512) 2359808  activation_41[0][0]        \n",
      "________________________________________________________________________________\n",
      "bn5a_branch2b (BatchNorma (None, 7, 7, 512) 2048     res5a_branch2b[0][0]       \n",
      "________________________________________________________________________________\n",
      "activation_42 (Activation (None, 7, 7, 512) 0        bn5a_branch2b[0][0]        \n",
      "________________________________________________________________________________\n",
      "res5a_branch2c (Conv2D)   (None, 7, 7, 2048 1050624  activation_42[0][0]        \n",
      "________________________________________________________________________________\n",
      "res5a_branch1 (Conv2D)    (None, 7, 7, 2048 2099200  activation_40[0][0]        \n",
      "________________________________________________________________________________\n",
      "bn5a_branch2c (BatchNorma (None, 7, 7, 2048 8192     res5a_branch2c[0][0]       \n",
      "________________________________________________________________________________\n",
      "bn5a_branch1 (BatchNormal (None, 7, 7, 2048 8192     res5a_branch1[0][0]        \n",
      "________________________________________________________________________________\n",
      "add_14 (Add)              (None, 7, 7, 2048 0        bn5a_branch2c[0][0]        \n",
      "                                                     bn5a_branch1[0][0]         \n",
      "________________________________________________________________________________\n",
      "activation_43 (Activation (None, 7, 7, 2048 0        add_14[0][0]               \n",
      "________________________________________________________________________________\n",
      "res5b_branch2a (Conv2D)   (None, 7, 7, 512) 1049088  activation_43[0][0]        \n",
      "________________________________________________________________________________\n",
      "bn5b_branch2a (BatchNorma (None, 7, 7, 512) 2048     res5b_branch2a[0][0]       \n",
      "________________________________________________________________________________\n",
      "activation_44 (Activation (None, 7, 7, 512) 0        bn5b_branch2a[0][0]        \n",
      "________________________________________________________________________________\n",
      "res5b_branch2b (Conv2D)   (None, 7, 7, 512) 2359808  activation_44[0][0]        \n",
      "________________________________________________________________________________\n",
      "bn5b_branch2b (BatchNorma (None, 7, 7, 512) 2048     res5b_branch2b[0][0]       \n",
      "________________________________________________________________________________\n",
      "activation_45 (Activation (None, 7, 7, 512) 0        bn5b_branch2b[0][0]        \n",
      "________________________________________________________________________________\n",
      "res5b_branch2c (Conv2D)   (None, 7, 7, 2048 1050624  activation_45[0][0]        \n",
      "________________________________________________________________________________\n",
      "bn5b_branch2c (BatchNorma (None, 7, 7, 2048 8192     res5b_branch2c[0][0]       \n",
      "________________________________________________________________________________\n",
      "add_15 (Add)              (None, 7, 7, 2048 0        bn5b_branch2c[0][0]        \n",
      "                                                     activation_43[0][0]        \n",
      "________________________________________________________________________________\n",
      "activation_46 (Activation (None, 7, 7, 2048 0        add_15[0][0]               \n",
      "________________________________________________________________________________\n",
      "res5c_branch2a (Conv2D)   (None, 7, 7, 512) 1049088  activation_46[0][0]        \n",
      "________________________________________________________________________________\n",
      "bn5c_branch2a (BatchNorma (None, 7, 7, 512) 2048     res5c_branch2a[0][0]       \n",
      "________________________________________________________________________________\n",
      "activation_47 (Activation (None, 7, 7, 512) 0        bn5c_branch2a[0][0]        \n",
      "________________________________________________________________________________\n",
      "res5c_branch2b (Conv2D)   (None, 7, 7, 512) 2359808  activation_47[0][0]        \n",
      "________________________________________________________________________________\n",
      "bn5c_branch2b (BatchNorma (None, 7, 7, 512) 2048     res5c_branch2b[0][0]       \n",
      "________________________________________________________________________________\n",
      "activation_48 (Activation (None, 7, 7, 512) 0        bn5c_branch2b[0][0]        \n",
      "________________________________________________________________________________\n",
      "res5c_branch2c (Conv2D)   (None, 7, 7, 2048 1050624  activation_48[0][0]        \n",
      "________________________________________________________________________________\n",
      "bn5c_branch2c (BatchNorma (None, 7, 7, 2048 8192     res5c_branch2c[0][0]       \n",
      "________________________________________________________________________________\n",
      "add_16 (Add)              (None, 7, 7, 2048 0        bn5c_branch2c[0][0]        \n",
      "                                                     activation_46[0][0]        \n",
      "________________________________________________________________________________\n",
      "activation_49 (Activation (None, 7, 7, 2048 0        add_16[0][0]               \n",
      "________________________________________________________________________________\n",
      "avg_pool (AveragePooling2 (None, 1, 1, 2048 0        activation_49[0][0]        \n",
      "================================================================================\n",
      "Total params: 23,587,712\n",
      "Trainable params: 23,534,592\n",
      "Non-trainable params: 53,120\n",
      "________________________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "summary(model)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   user  system elapsed \n",
       "  6.161   1.612   7.432 "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "t <- system.time(\n",
    "    features <- predict(model, x = fake_data, batch_size = params$BATCH_SIZE)\n",
    ")\n",
    "\n",
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] \"Images per second  172.228202368138\"\n"
     ]
    }
   ],
   "source": [
    "# Time execution on channels-first data\n",
    "print(paste(\"Images per second \", (params$BATCH_SIZE * params$BATCHES_GPU)/t[3]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "R",
   "language": "R",
   "name": "ir"
  },
  "language_info": {
   "codemirror_mode": "r",
   "file_extension": ".r",
   "mimetype": "text/x-r-source",
   "name": "R",
   "pygments_lexer": "r",
   "version": "3.4.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
